package com.skycong.hello77.activity.viewpager_own;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Window;
import com.skycong.hello77.R;
import com.skycong.hello77.activity.viewpager_own.view_layout.ViewPagerIndicator;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class MainActivity extends FragmentActivity {
    private ViewPager mViewPager;
    private ViewPagerIndicator mIndicator;
    private List<String> mTitles = Arrays.asList("短信1","推荐2","娱乐3","生活4");
    private List<VpSimpleFragment> mContent = new ArrayList<VpSimpleFragment>();
    private FragmentPagerAdapter mAdapter;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉头部的标题
        setContentView(R.layout.activity_view_pager);
        initDatas();//初始化数据
        initView();//初始化界面
        mViewPager.setAdapter(mAdapter);
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                //总的偏移量tabWidth*position+position*tabWidth
                mIndicator.scroll(position,positionOffset);

            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void initView() {
        mViewPager = (ViewPager) findViewById(R.id.id_viewpager);
        mIndicator = (ViewPagerIndicator) findViewById(R.id.id_indicator);

    }

    private void initDatas() {
        for(String title:mTitles){
            VpSimpleFragment fragment = VpSimpleFragment.newInstance(title);
            mContent.add(fragment);
        }
        mAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {//需要Manage
            @Override
            public Fragment getItem(int position) {
                return mContent.get(position);
            }

            @Override
            public int getCount() {
                return mContent.size();
            }
        };
    }
}
